package com.zscat.mallplus.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.sys.entity.SysRole;
import com.zscat.mallplus.sys.entity.SysUser;
import com.zscat.mallplus.sys.entity.SysUserRole;
import com.zscat.mallplus.sys.mapper.SysAppletSetMapper;
import com.zscat.mallplus.sys.mapper.SysRoleMapper;
import com.zscat.mallplus.sys.mapper.SysUserRoleMapper;
import com.zscat.mallplus.sys.service.ISysUserService;
import com.zscat.mallplus.ums.entity.SysAppletSet;
import com.zscat.mallplus.ums.entity.UmsAdminMember;
import com.zscat.mallplus.ums.service.IUmsAdminMemberService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.MeituanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 用户Appletset管理Controller
 */
@RestController
@Api(tags = "SysAppletSetController", description = "系统配置管理")
@RequestMapping("/api/applet")
@Slf4j
public class SysAppletSetController {

    @Resource
    private SysAppletSetMapper appletSetMapper;

    @IgnoreAuth
    @GetMapping(value = "/refreshMeituanSession")
    @ApiOperation(value = "刷新美团session")
    public Object refreshMeituanSession() {
        SysAppletSet appletSet = appletSetMapper.selectOne(new QueryWrapper<>());
        String refreshSession = appletSet.getMeituanRefreshSession();
        if(StringUtils.isNotBlank(refreshSession)) {
            String meituanAppId = appletSet.getMeituanAppid();
            String meituanAppSecret = appletSet.getMeituanAppsecret();
            String tokenContent = null;
            try {
                tokenContent = new MeituanUtil().refreshToken(meituanAppId, meituanAppSecret, refreshSession);
            } catch (IOException e) {
                e.printStackTrace();
                log.error("刷新美团token失败,异常:{}", e.getMessage());
            }
            JSONObject token = JSONObject.parseObject(tokenContent);
            if (token.getIntValue("code") == 200) {
                String session = token.getString("access_token");
                long expire_in = token.getLongValue("expires_in");
                appletSet.setMeituanSession(session);
                appletSet.setMeituanRefreshSession(refreshSession);
                appletSet.setExpiresIn(String.valueOf(expire_in));
                appletSetMapper.updateById(appletSet);
            }
        }
        return new CommonResult().success(appletSet);
    }
}
